package com.woniuxy.meal.controller;


import com.woniuxy.meal.common.Result;
import com.woniuxy.meal.entity.UserAddress;
import com.woniuxy.meal.form.AddNewAddrForm;
import com.woniuxy.meal.form.ModifyAddrForm;
import com.woniuxy.meal.service.UserAddressService;
import com.woniuxy.meal.service.dto.GetAddressByUserIdDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zz
 * @since 2023年06月12日
 */
@Api(tags = "地址模块相关接口")
@RestController
@RequestMapping("/userAddress")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    @ApiOperation("新增用户地址接口")
    @PostMapping("/newAddr")
    public Result addNewAddr(@Valid @RequestBody AddNewAddrForm form) throws Exception{
        UserAddress userAddress =  userAddressService.addNewAddr(form);
        return Result.success(200,"新增地址成功",userAddress);
    }

    @ApiOperation("修改地址信息接口")
    @PutMapping("/modifyAddr")
    public Result modifyAddr(@Valid  @RequestBody ModifyAddrForm form) throws Exception{
        userAddressService.modifyAddr(form);
        return Result.success(200,"修改地址信息成功",null);
    }

    @ApiOperation("获取当前登录用户所有地址信息")
    @GetMapping("/userAddress")
    public Result userAddress() throws Exception{
        List<GetAddressByUserIdDto> list =  userAddressService.getUserAddress();
        return Result.success(200,"获取当前用户地址信息成功",list);
    }



    @ApiOperation("删除地址信息")
    @DeleteMapping("/delAddress")
    @ApiImplicitParam(name = "addressId",value ="地址id" )
    public Result delAddress(@RequestParam Long addressId) throws Exception{
        userAddressService.delAddress(addressId);
        return Result.success(200,"删除地址信息成功",null);
    }

}

